
function checkImage(fileImg){

	img = fileImg.value;
	valid = new Array(".gif", ".jpg", ".png");
	if (img != "") {
		ext = (img.substring(img.lastIndexOf("."))).toLowerCase();
		check = false;
		for (var i = 0; i < valid.length; i++) {
			if (valid[i] == ext) {
				check = true;
				break;
			}
		}
		if (!check) {
			alert(ext + " La extensión del archivo de imagen no es válida!");
			fileImg.value="";			
		}
	}
}



function checkEmail(){
	document.getElementById("emailDiv").className="form-group has-warning";
	emailRequest=createRequest();

	if (emailRequest==null){
		alert("Unable to create request");
	}else{
		var theEmail= document.getElementById("email").value;
		var email= escape(theEmail);

		var url = "php/checkEmail.php?email=" + email;

		emailRequest.onreadystatechange= showEmailStatus;
		emailRequest.open("GET",url,true);
		emailRequest.send(null);
	}
}

function showEmailStatus(){

	if (emailRequest.readyState==4 && emailRequest.status==200){

		if(emailRequest.responseText=="okay"){
			
			document.getElementById("emailDiv").className="form-group has-success";
			$("#check").hide("slow");

		}else{
			$("#check").show("slow");
			document.getElementById("emailDiv").className="form-group has-error";
			document.getElementById("email").focus();
			document.getElementById("email").select();


		}

	}
}

function checkPassword(){

	var password1= document.getElementById("pswd");
	var password2= document.getElementById("pswd2");
	var passDiv= document.getElementById("passDiv");
	var passDiv2= document.getElementById("passDiv2");
	passDiv.className="form-group has-warning";
	passDiv2.className="form-group has-warning";

	//First compare two passwords

	if((password1.value=="")||(password2.value=="")||(password1.value!=password2.value)){

		passDiv.className="form-group has-error";
		passDiv2.className="form-group has-error";
		$("#checkPswd").show("slow");
		$("#tip").hide();
		password1.focus();
		password1.select();

		return;
	}

	//Password match, so send the request to the server

	passRequest= createRequest();
	if (passRequest==null){
		alert("unable to create request");
	}else{
		var password= escape(password1.value);
		var url= "php/checkPass.php?password?="+ password;
		passRequest.onreadystatechange= showPasswordStatus;
		passRequest.open("GET",url,true);
		passRequest.send(null);
	}
}
function showPasswordStatus(){
	if (passRequest.readyState==4){
		if(passRequest.status==200){
			var password1= document.getElementById("pswd");

			if(passRequest.responseText=="okay"){
				passDiv.className="form-group has-success";
				passDiv2.className="form-group has-success";
				$("#checkPswd").hide("slow");

			}else{
				$("#checkPswd").show("slow");
				passDiv.className="form-group has-error";
				passDiv.className="form-group has-error";
				password1.focus();
				password1.select();

			}

		}
	}
}


function addUser(){
	document.userForm.submit();

}

function validateFav(){
	if (document.getElementById("nombreFav").value==""){
		alert("Debe rellenar todos los campos obligatorios (*)");
		return;
	}



	var location=locationDisable;
	if(location==false){
		var e = document.getElementById("localidad");
		var select = e.options[e.selectedIndex].value;
		if (select==""){
			alert("Debe seleccionar la localidad en la que se efectuara el favor");
			return false;
		}
	}


	if(document.getElementById("descripcion").value.length<=30){
		alert("La descripción debe de contener al menos 30 caracteres");
		return false;
	}

	return true;
	//el formulario se ha validado correctamente y ahora procederemos a enviarlo
}

function locationDisable(){
	var e = document.getElementById("localidad");
	var radio = document.getElementsByName("tipoNewFav");
	var tipo="";
	for ( var i = 0; i < radio.length; i++) {
		if(radio[i].checked){
			tipo=radio[i].value;
		}
	}
	if(tipo=="online"){
		$("#localidadDiv").hide("slow");

		return true;
	}else{
		$("#localidadDiv").show("slow");
		e.disabled=false;
	}
}

function locationDisableEdit(){

	var e = document.getElementById("localidadEdit");
	var radio = document.getElementsByName("tipoEdit");
	var tipo="";
	for ( var i = 0; i < radio.length; i++) {
		if(radio[i].checked){
			tipo=radio[i].value;
		}
	}

	if(tipo=="online"){
		$("#localidadDivEdit").hide("slow");

		return true;
	}else{
		$("#localidadDivEdit").show("slow");
		e.disabled=false;
	}
}
function validarEdit(){
	var location=locationDisableEdit;
	if(location==false){
		var e = document.getElementById("localidadEdit");
		var select = e.options[e.selectedIndex].value;
		if (select==""){
			alert("Debe seleccionar la localidad en la que se efectuara el favor");
			return;
		}
	}
	var nombre=document.getElementsByName("editNombre");
	var tipo=document.getElementsByName("editTipo");
	var categoria=document.getElementsByName("editCategoria");
	var descripcion=document.getElementsByName("editDescripcion");

	if(nombre.value==""||tipo.value==""||categoria.value==""||descripcion.value==""){
		return false;
	}else{
		return true;
	}
}

function validateUsr(){

}

function iniciar(){
	$("#exitoPswd").hide();
	$("#fracasoPswd").hide();
	$("#newCheckPswd").hide();
	$("#formChangePswd").show();
	$("#submitUsrPswd").click(function(){
		
		$.post("changePswd.php",$("#changeUsrPswd").serialize(), function(res){
			if(res==0){
				$("#formChangePswd").fadeOut();
				$("#exitoPswd").fadeIn("slow");
			}else{
				alert(res);
				$("#formChangePswd").fadeOut();
				$("#fracasoPswd").fadeIn("slow");
			}
		});
	});
}
function checkUsrPassword(){

	var password1= document.getElementById("newPswd");
	var password2= document.getElementById("newPswd2");
	var passDiv= document.getElementById("newPassDiv");
	var passDiv2= document.getElementById("newPassDiv2");
	passDiv.className="form-group has-warning";
	passDiv2.className="form-group has-warning";

	//First compare two passwords

	if((password1.value=="")||(password2.value=="")||(password1.value!=password2.value)){

		passDiv.className="form-group has-error";
		passDiv2.className="form-group has-error";
		$("#newCheckPswd").show("slow");
		$("#tip").hide();
		password1.focus();
		password1.select();

		return;
	}

		
}

